package com.right.health.app.ui.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.utils.VerifyUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.net.UserApi;

/*
 * Created by hopeliao on 2019-09-25
 *
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private View[] mTypeViews;

    private EditText mContentEt;
    private int mChooseType = -1;
    private EditText mPhoneEt;

    private UserApi mUserApi;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.feedback);
    }

    @Override
    public void findView() {
        findViewById(R.id.tv_type1).setOnClickListener(this);
        findViewById(R.id.tv_type2).setOnClickListener(this);
        findViewById(R.id.tv_type3).setOnClickListener(this);

        findViewById(R.id.tv_save).setOnClickListener(this);

        mContentEt = (EditText) findViewById(R.id.et_content);
        mPhoneEt = (EditText) findViewById(R.id.et_phone);
        mTypeViews = new View[3];

        mTypeViews[0] = findViewById(R.id.tv_type1);
        mTypeViews[1] = findViewById(R.id.tv_type2);
        mTypeViews[2] = findViewById(R.id.tv_type3);
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_type1:
            case R.id.tv_type2:
            case R.id.tv_type3:
                refreshView(v);
                break;
            case R.id.tv_save:
                submit();
                break;
        }
    }

    private void submit() {
        if (mChooseType == -1) {
            ToastUtil.show(this, "请选择反馈类型");

            return;
        }
        String content = mContentEt.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtil.show(this, R.string.feedback_please_enter);
            return;
        }
        String phone = mPhoneEt.getText().toString().trim();
        if (TextUtils.isEmpty(phone) || !VerifyUtil.isMobile(phone)) {
            ToastUtil.show(this, R.string.please_enter_phone);
            return;
        }

        mUserApi.feedback(mChooseType, content, phone);
    }

    private void refreshView(View view) {
        for (int i = 0; i < mTypeViews.length; i++) {
            View typeView = mTypeViews[i];
            if (view == typeView) {
                mChooseType = i;
                typeView.setSelected(true);
            } else {
                typeView.setSelected(false);
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseOK()) {
            ToastUtil.show(this, "反馈成功，谢谢。");
            finish();
        } else {
            ToastUtil.show(this, "反馈失败,请稍后再试");
        }
    }
}
